package com.medusa.gruul.payment.service.service.impl;

import com.medusa.gruul.common.model.enums.PayType;
import com.medusa.gruul.common.system.model.ISystem;
import com.medusa.gruul.common.web.handler.Handler;
import com.medusa.gruul.common.web.util.SpringUtils;
import com.medusa.gruul.payment.api.entity.MerchantDetails;
import com.medusa.gruul.payment.service.common.annotation.MerchantHandler;
import com.medusa.gruul.payment.service.common.helper.WechatHelper;
import com.medusa.gruul.payment.service.model.dto.MerchantDetailsDTO;
import com.medusa.gruul.payment.service.model.enums.PaymentError;
import com.medusa.gruul.payment.service.model.vo.MerchantDetailsVO;
import com.medusa.gruul.payment.service.mp.service.IMerchantDetailsService;
import com.medusa.gruul.payment.service.mp.service.IPaymentMerchantConfigService;
import com.medusa.gruul.payment.service.service.PaymentMerchantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author xiaoq
 * @ Description
 * @date 2022-07-13 09:14
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PaymentMerchantServiceImpl implements PaymentMerchantService {

	private final IMerchantDetailsService merchantDetailsService;

	private final IPaymentMerchantConfigService paymentMerchantConfigService;

	/**
	 * 编辑(新增/或修改)商户支付配置
	 *
	 * @param paymentMerchantDetailsDTO 支付商户渠道配置DTO
	 */
	@Override
	public void editPaymentMerchant(MerchantDetailsDTO paymentMerchantDetailsDTO) {
		Handler<Void> handler = SpringUtils.getBean(MerchantHandler.class, paymentMerchantDetailsDTO.getPayType());
		handler.handle(paymentMerchantDetailsDTO);
	}

	/**
	 * 获取商户支付配置
	 *
	 * @param payType 支付渠道
	 * @return 商户支付配置
	 */
	@Override
	public List<MerchantDetailsVO> getMerchantDetail(PayType payType) {
		return paymentMerchantConfigService.getMerchantDetail(payType);
	}

	/**
	 * 支付证书上传
	 *
	 * @param file 证书文件
	 */
	@Override
	public String uploadCertificate(MultipartFile file, String detailsId) {
		if (detailsId != null) {
			MerchantDetails merchantDetails = merchantDetailsService.getById(detailsId);
			if (merchantDetails == null) {
				throw PaymentError.PAYMENT_CERTIFICATE_UPLOAD_ERROR.exception();
			}
		}
		String originalFilename = file.getOriginalFilename();
		if (!WechatHelper.nameVerify(originalFilename)) {
			throw PaymentError.PAYMENT_CERTIFICATE_FORMAT_ERROR.exception();
		}
		String absolutePath = WechatHelper.absolutePath(ISystem.shopIdMust().toString(),originalFilename);
		log.debug("支付证书存放路径:{}", absolutePath);
		try {
			file.transferTo(new File(absolutePath));
		} catch (IOException exception) {
			log.error("保存文件发生错误", exception);
			throw PaymentError.PAYMENT_CERTIFICATE_UPLOAD_ERROR.exception();
		}
		return absolutePath;
	}
}
